package com.cakeclient.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class OurSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "cakeathomedb";
    private static final int DATABASE_VERSION = 10;

    public static final DatabaseTable[] DATABASE_TABLES = new DatabaseTable[]{
            Client_has_Order.getInstance(),
            ClientTable.getInstance(),
            DeliveryTypeTable.getInstance(),
            LocalPriceHTTable.getInstance(),
            Menu_has_Product.getInstance(),
            MenuTable.getInstance(),
            MenuTypeTable.getInstance(),
            MenuType_has_LocalPriceHT.getInstance(),
            MenuType_has_ProductCategory.getInstance(),
            MenuType_has_ProductType.getInstance(),
            MenuType_has_RecommendedPrice.getInstance(),
            Order_has_UnitarySale.getInstance(),
            OrderTable.getInstance(),
            PaymentTypeTable.getInstance(),
            PlaceTable.getInstance(),
            ProductCategory_has_ProductCategory.getInstance(),
            ProductCategoryTable.getInstance(),
            ProductTable.getInstance(),
            ProductType_has_LocalPriceHT.getInstance(),
            ProductType_has_ProductCategory.getInstance(),
            ProductType_has_RecommendedPrice.getInstance(),
            ProductTypeTable.getInstance(),
            RecommendedPriceTable.getInstance(),
            StoreTable.getInstance(),
            UnitarySaleTable.getInstance(),
            VATCategoryTable.getInstance()
    };


    public OurSQLiteOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        for (DatabaseTable dbt : DATABASE_TABLES) {

            database.execSQL(dbt.getCreate());

        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        for (DatabaseTable dbt : DATABASE_TABLES) {
            try {
                database.execSQL("DROP TABlE " + dbt.getTableName());
            } catch (Exception e) {

            }
        }
        for (DatabaseTable dbt : DATABASE_TABLES) {

            database.execSQL(dbt.getCreate());

        }
    }

    public void truncateTable() throws Exception {
        truncateTable(DATABASE_TABLES);
    }

    public void truncateTable(DatabaseTable[] tables) {
        for (DatabaseTable dbt : tables) {
            this.getWritableDatabase().execSQL("DELETE FROM " + dbt.getTableName());
        }
    }

    public void truncateTable(DatabaseTable table) {
        this.getWritableDatabase().execSQL("DELETE FROM " + table.getTableName());
    }

}
